package com.shyroke.daydayzhuanweb.controller;

import com.shyroke.daydayzhuanapi.dto.InviteImgAdminDto;
import com.shyroke.daydayzhuanapi.dto.LayEditRespon;
import com.shyroke.daydayzhuansvc.service.impl.InviteImgService;
import com.shyroke.daydayzhuansvc.service.impl.OSSService;
import com.shyroke.daydayzhuansvc.util.ExceptionUtil;
import com.shyroke.daydayzhuansvc.util.OSSClientConstants;
import com.shyroke.daydayzhuansvc.util.Page;
import com.shyroke.daydayzhuansvc.util.TabelData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping(value = "admin/inviteImg")
public class InviteImgAdminController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private InviteImgService service;
    @Autowired
    private OSSService ossService;

    @RequestMapping(value = "listView")
    public ModelAndView siteMap (ModelAndView model){
        model.setViewName("admin/inviteImg/list");
        return  model;
    }


    @GetMapping(value = "list")
    @ResponseBody
    public TabelData<InviteImgAdminDto> page(Page page){
        return service.page(page);
    }


    @RequestMapping(value = "{id}/view")
    public ModelAndView detail(@PathVariable Integer id, ModelAndView model){
        model.addObject("id", id);
        model.setViewName("admin/inviteImg/edit");
        return  model;
    }

    @GetMapping("{id}")
    @ResponseBody
    public InviteImgAdminDto detail(@PathVariable Integer id) {
        return service.getOne(id);
    }

    @PostMapping("")
    @ResponseBody
    public void save(@Validated @RequestBody InviteImgAdminDto dto) {
        service.save(dto);
    }

    @PutMapping("{id}")
    @ResponseBody
    public void update(@PathVariable Integer id, @Validated @RequestBody InviteImgAdminDto dto) {
        service.update(id, dto);
    }


    /**
     * 保存项目内容的图片
     * @param file
     * @return
     */
    @RequestMapping(value = "{id}/uploadImg")
    @ResponseBody
    public LayEditRespon uploadProjectImg(@PathVariable Integer id, MultipartFile file){
        LayEditRespon respon = new LayEditRespon();

        if(file.isEmpty()){
            respon.setCode(-1);
            respon.setMsg("图片为空");
            return respon;
        }

        InviteImgAdminDto dto = service.getOne(id);
        String oldUrl = dto.getUrl();
        String oldFileName = oldUrl.substring(oldUrl.lastIndexOf("/") + 1);

        String path = OSSClientConstants.INVITE_IMG_PATH + oldFileName;
        String url;
        try {
            ossService.uploadImg(file,path);
            url = ossService.getImgUrl(path);
        }catch (Exception e){
            logger.info(ExceptionUtil.getMessage(e));
            respon.setCode(-1);
            respon.setMsg("图片上传失败");
            return respon;
        }
        Map<String,Object> data = new HashMap<String,Object>();
        data.put("src",url);
        respon.setCode(0);
        respon.setMsg("保存成功");
        respon.setData(data);
        return respon;
    }


}
